package com.dudu.utils;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * JSON解析工具类
 * @author zx
 *
 */
public class JSONUtils {

	private static final JSONArray EMPTY_JSONARRAY = new JSONArray();

	private JSONUtils() {

	}
    
	/**
	 * 获取json数组对象，如果传入null或空串，返回空的jsonarray
	 * @param object 要解析的json，类型为字符串
	 * @return 
	 */
	public static JSONArray getJSONArrayOrEmpty(String object) {
		JSONArray jArray = null;
		if (StringUtils.isEmpty(object))
			return EMPTY_JSONARRAY;
		try {
			jArray = JSONArray.fromObject(object);
		} catch (Exception err) {
		}
		return jArray;
	}
    
	/**
	 * 返回json对象
	 * @param object
	 * @return
	 */
	public static JSONObject getJSON(String object) {
		JSONObject jObject = null;
		if (StringUtils.isEmpty(object))
			return jObject;
		try {
			jObject = JSONObject.fromObject(object);
		} catch (Exception err) {
		}
		return jObject;
	}

}
